from meta import componentMeta
from factory import cfactory

class component(object):
    _name = "component"
    requires = []
    __metaclass__ = componentMeta

    def onAttach(self, obj):
        """
        This is called when a component is attached to an entity object. It will be called -after- the component is added to the components list.
        """
        pass

    @staticmethod
    def calculateDependencies(name, deps):
        comp = cfactory.get(name)
        if comp == None:
            raise ValueError("the specified component %s does not exist." %(name))

        deps.append(name)
        for requirement in comp.requires:
            if requirement in deps:
                raise ValueError("Recursive dependencies. %s is already required." %(requirement))
            deps = component.calculateDependencies(requirement, deps)

        return deps
